<?php
/**
 *不参与正常抽奖逻辑，只是管理员后台觉得用户亏的太多给用户发放的奖品
 */

namespace App\Modules\LuckDraw\Strategy;

use App\Modules\LuckDraw\Award\AwardFactory;
use App\Modules\LuckDraw\Models\ManualAwardModel;

class ManualStrategy {
    private $_luckId;
    private $_userId;
    private $_pool;
    private $_mids;
    public function __construct(int $luckId,int $userId, array $pool){
        $this->_luckId = $luckId;
        $this->_userId = $userId;
        $this->_pool =  [];
        foreach ($pool as $p) {
            array_push($this->_pool, $p->type . '-' . $p->id . '-' . $p->time);
        }
    }

    public function run($num): array{
        $manualAwards = ManualAwardModel::get($this->_luckId, $this->_userId, $num);
        $awards = [];
        $this->_mids = [];
        foreach ($manualAwards as $ma) {
            $k = $ma->type . '-' . $ma->award_id . '-' . $ma->time_limit;
            if (in_array($k, $this->_pool)){
                $award = AwardFactory::get($this->_luckId, $this->_userId, $ma->type,$ma->award_id,$ma->time_limit, $ma->price);
                $award->num = 2;
                array_push($awards, $award);
                array_push($this->_mids, $ma->id);
                continue;
            }
            self::setHasBeenDeleted($ma->id);
        }
        return $awards;
    }

    public function setHasBeenDeleted(int $id): bool {
        return ManualAwardModel::setHasBeenDeleted($id)>0;
    }

    public function setHasBeenSent(int $drawId): bool {
        //如果为空不用保存，直接返回
        if (empty($this->_mids)){
            return true;
        }
        return ManualAwardModel::setHasBeenSentByIds($drawId, $this->_mids)>0;
    }
}